package com.example.foodsy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foodsy.dto.AddressDTO;
import com.example.foodsy.dto.Result;
import com.example.foodsy.utils.Result;
import com.example.foodsy.entity.SlauInfo;
import com.example.foodsy.entity.WholInfo;
import com.example.foodsy.service.SlauInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 屠宰企业产品批号信息表 前端控制器
 * </p>
 *
 * @author cgc
 * @since 2025-09-23
 */
@RestController
@RequestMapping("/slau-info")
@CrossOrigin
public class SlauInfoController {
	@Autowired
	private SlauInfoService slauInfoService;

	//通过企业编号来查询对应企业所有未确认的批号信息
	@GetMapping("/listByfbId/{fbId}")
	public Result listByfbId(@PathVariable Integer fbId){
		return Result.ok(slauInfoService.listByfbId(fbId));
	}

	//通过企业名称进行模糊查询
	@GetMapping("/listBySlauName/{slauName}")
	public Result listBySlauName(@PathVariable(required = false) String slauName){
		return Result.ok(slauInfoService.listBySlauName(slauName));
	}

	//根据企业状态进行查询
	@GetMapping("/listByState/{state}/{sbId}")
	public Result listByState(@PathVariable Integer state,@PathVariable Integer sbId){
		return Result.ok(slauInfoService.list(new QueryWrapper<SlauInfo>().eq("state", state).eq("sb_id",sbId)));
	}

	//根据屠宰企业编号查询
	@GetMapping("/getinfoBySlauId/{slauId}")
	public Result getinfoBySlauId(@PathVariable Integer slauId){
		return Result.ok(slauInfoService.getById(slauId));
	}


	//根据产品批号查询
	@GetMapping("/getSlauInfoByBatchId/{batchId}")
	public Result getinfoByBatchId(@PathVariable String batchId){
		return Result.ok(slauInfoService.getOne(new QueryWrapper<SlauInfo>().eq("batch_id", batchId)));
	}





	//删除
	@DeleteMapping("/removeByBatchId/{BatchId}")
	public Result removeBySlauId(@PathVariable Integer BatchId){

		return Result.ok(slauInfoService.remove(new QueryWrapper<SlauInfo>().eq("batch_id", BatchId)));
	}

	//修改
	@PutMapping("/editSlauInfo")
	public Result editSlauInfo(@RequestBody SlauInfo slauInfo){
		return Result.ok(slauInfoService.updateById(slauInfo));
	}

	//添加
	@PostMapping("/addSlauInfo")
	public Result addSlauInfo(@RequestBody SlauInfo slauInfo){

		return slauInfoService.addSlauInfo(slauInfo)?Result.ok():Result.fail("添加失败");
	}

	@GetMapping("getSlauInfoBystate/{state}")
	public Result getSlauInfoBystate(@PathVariable Integer state){
		List<AddressDTO<SlauInfo>> slauInfoBystate = slauInfoService.getSlauInfoBystate(state);

		return Result.ok(slauInfoBystate);
	}

	//将待确认状态修改成已确认状态
	@PostMapping("/updateState/{batchId}")
	public Result updateState(@PathVariable String batchId){
		SlauInfo slauInfo1 = slauInfoService.getOne(new QueryWrapper<SlauInfo>().eq("batch_id", batchId));
		slauInfo1.setState(3);
		return slauInfoService.updateById(slauInfo1)?Result.ok():Result.fail("修改失败");
	}

	//将已确认状态修改成下架状态
	@PostMapping("/delete/{batchId}")
	public Result delete(@PathVariable String batchId){
		SlauInfo slauInfo1 = slauInfoService.getOne(new QueryWrapper<SlauInfo>().eq("batch_id", batchId));
		slauInfo1.setState(4);
		return slauInfoService.updateById(slauInfo1)?Result.ok():Result.fail("修改失败");
	}

	@GetMapping("/getInfoBysbId/{sbId}")
	public Result getInfoBywholId(@PathVariable Integer sbId){
		AddressDTO<SlauInfo> addressDTO = slauInfoService.getInfoBysbId(sbId);

		return Result.ok(addressDTO);
	}



}
